package com.xiaoyuanxing.demo.controller;

import com.github.pagehelper.Page;
import com.xiaoyuanxing.demo.entity.Account;
import com.xiaoyuanxing.demo.entity.Order;
import com.xiaoyuanxing.demo.pojo.PageInfo;
import com.xiaoyuanxing.demo.service.AccountService;
import com.xiaoyuanxing.demo.service.OrderService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Timestamp;

/*
 **@author LuoShengYing
 **@date 2021/5/23
 */
@RestController
@RequestMapping("Order")
public class OrderController {
    @Resource(name = "accountService")
    private AccountService accountService;

    @Resource(name = "orderService")
    private OrderService orderService;

    /*
    * 查询可接取(Status == -1)的订单数组
    * */
    @RequestMapping("getOrders")
    public PageInfo<Order> getOrders(@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                                     @RequestParam(defaultValue = "10",value = "pageSize") int pageSize){
        Page<Order> orders = orderService.getOrdersByStatus(-1,pageNum,pageSize);
        return new PageInfo<>(orders);
    }

    /*
     * 查看账户创建的订单
     * */
    @RequestMapping("getOrderByCreatorID")
    public PageInfo<Order> getOrderByCreatorID(String openid, @RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                                               @RequestParam(defaultValue = "10",value = "pageSize") int pageSize){
        // 获取openID对应的Account数据
        Account account = accountService.selectByOpenID(openid);
        Page<Order> creatorOrders = orderService.getOrderIDsByCreatorID(account.getAccountID(), pageNum, pageSize);
        return new PageInfo<>(creatorOrders);
    }

    /*
     * 查看账户接取的订单
     * */
    @RequestMapping("getOrderByTakerID")
    public PageInfo<Order> getOrderByTakerID(String openid, @RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                                         @RequestParam(defaultValue = "10",value = "pageSize") int pageSize){
        // 获取openID对应的Account数据
        Account account = accountService.selectByOpenID(openid);
        Page<Order> takerOrders = orderService.getOrderIDsByTakerID(account.getAccountID(), pageNum, pageSize);
        return new PageInfo<>(takerOrders);
    }

    /*
    * 接单
    * */
    @RequestMapping("takingOrder")
    public boolean takingOrder(String openid, int orderID){
        // 获取openID对应的Account数据
        Account account = accountService.selectByOpenID(openid);
        // 如果openID对应的Account数据为空，则说明数据库中不存在此用户
        if (ObjectUtils.isEmpty(account)){
            return false;
        }
        // 获取orderID对应的Order数据
        Order order = orderService.getOrderByID(orderID);
        // 判断订单状态是否为-1，否则接单失败
        if (order.getStatus() != -1){
            return false;
        }
        return orderService.takeOrder(orderID, account.getAccountID());
    }

    /*
    * 更新订单状态为 "已完成"
    * */
    @RequestMapping("completeOrder")
    public boolean updateStatusAsFinish(int orderID) {
        return orderService.updateStatusAsFinish(orderID);
    }


    /*
    * 新建订单
    * */
    @RequestMapping("createNewOrder")
    public boolean createNewOrder(String openid, String comment, String address, double price){
        // 获取openID对应的Account数据
        Account account = accountService.selectByOpenID(openid);
        // 创建Order对象存储数据
        Order order = new Order();
        order.setCreatorID(account.getAccountID());
        order.setAddress(address);
        order.setComment(comment);
        order.setStatus(-1);
        order.setPrice(price);
        order.setCreatedTime(new Timestamp(System.currentTimeMillis()));
        orderService.createNewOrder(order);

        return !ObjectUtils.isEmpty(order.getOrderID());
    }

    /*
    * 删除订单
    * */
    @RequestMapping("deleteOrder")
    public boolean deleteOrder(int orderID){
        return orderService.deleteOrder(orderID);
    }
}
